package org.cloudie.docs.editor.client.extended;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;

/**
 * A command event.
 */
public class FileDropEvent extends GwtEvent<FileDropHandler> {

  /**
   * The event type.
   */
  private static Type<FileDropHandler> TYPE;

  /**
   * Fires a command event on all registered handlers in the handler source.
   * 
   * @param <S> The handler source
   * @param source the source of the handlers
   * @param nativeEvent the native event
   */
  public static <S extends HasFileDropHandlers & HasHandlers> void fire(S source,
	    FileDropNativeEvent nativeEvent) {
    if (TYPE != null) {
      FileDropEvent event = new FileDropEvent(nativeEvent);
      source.fireEvent(event);
    }
  }

  /**
   * Ensures the existence of the handler hook and then returns it.
   * 
   * @return returns a handler hook
   */
  public static Type<FileDropHandler> getType() {
    if (TYPE == null) {
      TYPE = new Type<FileDropHandler>();
    }
    return TYPE;
  }

  private FileDropNativeEvent nativeEvent;
  
  /**
   * Construct a new {@link FileDropEvent}.
   */
  protected FileDropEvent(FileDropNativeEvent nativeEvent) {
	this.nativeEvent = nativeEvent;
  }

  @Override
  public final Type<FileDropHandler> getAssociatedType() {
    return TYPE;
  }

  public FileDropNativeEvent getNativeEvent() {
    return nativeEvent;
  }

  @Override
  public String toDebugString() {
    assertLive();
    return super.toDebugString();
  }

  @Override
  protected void dispatch(FileDropHandler handler) {
    handler.onFileDrop(this);
  }
  

  public static class FileDropNativeEvent extends JavaScriptObject {
	
	protected FileDropNativeEvent() { }
	
	public final native void stopPropagation() /*-{
	  this.stopPropagation();
	}-*/;
	
	public final native void preventDefault() /*-{
	  this.preventDefault();
	}-*/;
	
	public final native FileDropEventDataTransfer getDataTransfer() /*-{
	  return this.dataTransfer;
	}-*/;
	
	public static class FileDropEventDataTransfer extends JavaScriptObject {
	  
	  protected FileDropEventDataTransfer() { }
	  
	  public final native JsArray<File> getFiles() /*-{
	    return this.files;
	  }-*/;
	  
	}
  }
}
